//
//  AddModifyCarViewController.m
//  KuWeiPark
//
//  Created by Kevin Su on 15/5/6.
//  Copyright (c) 2015年 Clief. All rights reserved.
//

#import "AddModifyCarViewController.h"
#import "AppMacro.h"
#import "SVProgressHUD.h"
#import "HttpClientManager.h"
#import "DataModel.h"
#import "AppDelegate.h"

@interface AddModifyCarViewController ()

@end

@implementation AddModifyCarViewController{
    
    NSArray *provinceArray;
    NSString *isPrimary;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.saveBtn.layer.masksToBounds = YES;
    self.saveBtn.layer.cornerRadius = 2;
    
    [self.selectAsMainBtn addTarget:self action:@selector(selectedAsMainCarAction:) forControlEvents:UIControlEventTouchUpInside];
    
    if(self.isModeify){
        
        self.navigationItem.title = @"编辑车牌号";
        self.tipLabel.text = @"编辑车牌号";
        
        self.carNoTextField.text = self.carModel.carnumber;
        if([self.carModel.isPrimary integerValue] == 0){
            
            self.selectAsMainBtn.selected = NO;
            isPrimary = @"0";
        }else{
            
            self.selectAsMainBtn.selected = YES;
            isPrimary = @"1";
        }
        
    }else{
        
        self.navigationItem.title = @"新增车牌号";
        self.tipLabel.text = @"新增车牌号";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - IBActions

- (IBAction)saveAction:(id)sender {
    
    [self addCarRequest];
}

- (IBAction)back:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)selectedAsMainCarAction:(UIButton *)button{
    
    if(button.selected){
        
        button.selected = NO;
        isPrimary = @"0";
    }else{
        
        button.selected = YES;
        isPrimary = @"1";
    }
}

#pragma mark - 添加车牌号
- (void)addCarRequest{
    
    NSString *carNoStr = self.carNoTextField.text;
    if(carNoStr == nil || carNoStr.length == 0){
        
        [SVProgressHUD showErrorWithStatus:@"请输入车牌号"];
        return;
    }
    
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
    [[HttpClientManager sharedClient] addCarRequestWithMobilePhone:ApplicationDelegate.loginModel.mobilePhone WithUserId:ApplicationDelegate.loginModel.userId WithProvincename:[carNoStr substringWithRange:NSMakeRange(0, 1)] Withcarnumbersuffix:[carNoStr substringWithRange:NSMakeRange(1, carNoStr.length - 1)] WithIsprimary:isPrimary WithSucessBlock:^(JsonRetMsg *response) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD showSuccessWithStatus:@"添加成功"];
            
            if([isPrimary integerValue] == 1){
                
                //更新本地存储的用户车牌
                [self updateLocalLoginModelWithCarNo:carNoStr];
            }
            
            [self.navigationController popViewControllerAnimated:YES];
        });
        
    } withFailedBlock:^(JsonRetMsg *responseObject) {
        
    }];
}

- (void)updateLocalLoginModelWithCarNo:(NSString *)carNo{
    
    LoginModel *loginModel = ApplicationDelegate.loginModel;
    loginModel.carNumber = carNo;
}

@end
